﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScheduleMaker.DB.Domain.Core;
using ScheduleMaker.DB.CoreDAO;
using ScheduleMaker.DB;
using ScheduleMaker.ConfigManager;
using ScheduleMaker.Converter;

namespace ScheduleMaker.ScheduleManager
{
    public class ScheduleManager
    {

        static ScheduleDAO scheduleDAO;
        static EmergencyScheduleDAO emergencyScheduleDAO;

        static ScheduleManager()
        {
            Config config = ConfigManager.ConfigManager.getConfiguration();
            scheduleDAO = DAOFactory.getScheduleDAO(config.Type);
            emergencyScheduleDAO = DAOFactory.getEmergencyScheduleDAO(config.Type);
        }

        public static IList<Schedule> getScheduleForDay(Day day)
        {
            IList<Schedule> schedule = new List<Schedule>();
            DayOfWeek dayOfWeek = DomainConverter.getDayOfWeek(day);
            DateTime date = System.DateTime.Now;
            /*if (date.DayOfWeek == dayOfWeek)
            {
            }
            else
            {
            }*/
            return schedule;
        }

        public static IList<Schedule> getWeeklySchedule() {
            IList<Schedule> schedule = new List<Schedule>();
            DateTime date = System.DateTime.Today;
            DayOfWeek today = date.DayOfWeek;

            double diffToMonday = 0;
            switch (today)
            {
                case DayOfWeek.Tuesday:
                    diffToMonday = -1;
                    break;
                case DayOfWeek.Wednesday:
                    diffToMonday = -2;
                    break;
                case DayOfWeek.Thursday:
                    diffToMonday = -3;
                    break;
                case DayOfWeek.Friday:
                    diffToMonday = -4;
                    break;
                case DayOfWeek.Saturday:
                    diffToMonday = -5;
                    break;
                case DayOfWeek.Sunday:
                    diffToMonday = -6;
                    break;
            }

            DateTime begin = date.AddDays(diffToMonday);
            for (int i = 0; i < 7; i++)
            {
                DateTime dateTime = begin.AddDays(i);
                //TimeSpan time = new TimeSpan(dateTime.Ticks);
                List<EmergencySchedule> emergencySchedule = 
                    new List<EmergencySchedule>(emergencyScheduleDAO.getByDate(dateTime));
                if (emergencySchedule.Count == 0)
                {
                    IList<Schedule> currSchedule = 
                        scheduleDAO.getByDay(DomainConverter.getDay(dateTime.DayOfWeek));
                    schedule = new List<Schedule>(schedule.Concat<Schedule>(currSchedule));
                }
                else
                {
                    IList<Schedule> currSchedule = 
                        emergencySchedule.ConvertAll<Schedule>(x => new EmergencyScheduleAdapter(x));
                    schedule = new List<Schedule>(schedule.Concat<Schedule>(currSchedule));
                }
            }

            return schedule;
        }

    }
}
